{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1ead74ba-b624-49f8-9468-51c8f3617ae0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 感知机\n",
    "# 与门(2.1式)\n",
    "def AND(x1, x2):\n",
    "    w1, w2, theta = 0.5, 0.5, 0.7\n",
    "    tmp = x1 * w1 + x2 * w2\n",
    "    if tmp <= theta:\n",
    "        return 0\n",
    "    return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "8ab13983-07a6-44e2-9eea-81cccb5e3de1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 结果\n",
    "AND(0, 0) # 0\n",
    "AND(1, 0) # 0\n",
    "AND(0, 1) # 0\n",
    "AND(1, 1) # 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "48aba352-e431-43a2-84f3-980674d7813c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.19999999999999996"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 与门(2.2式) theta = -b\n",
    "import numpy as np\n",
    "x = np.array([0, 1]) # 输入\n",
    "w = np.array([0.5, 0.5]) # 权重\n",
    "b = -0.7 # 偏执\n",
    "np.sum(w*x) + b # w*x = array([0., 0.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "567a96a0-4a7e-4637-90f6-be122d03439f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 非门\n",
    "def NAND(x1,  x2):\n",
    "    x = np.array([x1, x2])\n",
    "    w = np.array([-0.5, -0.5]) # 只有偏执和权重为AND取反\n",
    "    b = 0.7\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ba6021da-7aae-4103-a50f-bb9317b5ed1c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 或门\n",
    "def OR(x1, x2):\n",
    "    x = np.array([x1, x2])\n",
    "    w = np.array([0.5, 0.5])\n",
    "    b = -0.2\n",
    "    tmp = np.sum(w*x) + b\n",
    "    if tmp <= 0:\n",
    "        return 0\n",
    "    return 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8fd0d2e4-3989-45ea-a716-e5ea86eca350",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 异或门(多层感知机)\n",
    "def XOR(x1, x2):\n",
    "    s1 = NAND(x1, x2)\n",
    "    s2 = OR(x1, x2)\n",
    "    y = AND(s1, s2)\n",
    "    return y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a8771a9b-13ad-4c06-a604-86b5f49c9810",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "XOR(0, 0) # 0\n",
    "XOR(0, 1) # 1\n",
    "XOR(1, 0) # 1\n",
    "XOR(1, 1) # 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "564571d0-eab7-4396-84ee-a2496416c4b5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
